package network.producer;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * 生产者
 *
 * @author 龙小虬
 * @date 2021/5/6 22:46
 */
public class ProducerHandler extends SimpleChannelInboundHandler<ByteBuf> {
    /**
     * 生产者向MQ服务器端投递消息缓存
     * 报文格式 type code data
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "producer");
        jsonObject.put("code", "200");
        JSONObject object = new JSONObject();
        object.put("name", "lxq");
        object.put("phone", "17700770000");
        jsonObject.put("data", object);
        // 向MQ服务器通讯
        ctx.writeAndFlush(Unpooled.copiedBuffer(jsonObject.toJSONString(), CharsetUtil.UTF_8));
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {

    }
}